
import AbsMenu from '../absmenu.js'
import { FileAccessSvc } from '../../../manage/service/FileAccessSvc.js';
import { LocalFileSvc } from '../../../manage/service/LocalFileSvc.js';

class FileMenu extends AbsMenu.AbsMenu {
    constructor(map, item) {
        super(map, item);
    }

    act2Click() {
        switch (this.menuSubItem) {
            case 'newFile':
                this.newFile();
                break;
            case 'newProject':
                this.newProject();
                break;

            case 'open':
                this.openFile();
                break;
            case 'save':
                FileAccessSvc.saveFile();
                break;
            case 'saveas':
                FileAccessSvc.saveToLocal();
                break;
            case 'export':
                break;

            case 'logout':
                break;
            default:
                break;
        }
    }

    selectFile(accept, formatArr) {
        return new Promise((resolve, reject) => {
            const fileInput = document.createElement('input')
            fileInput.type = 'file';
            fileInput.accept = accept;
            fileInput.click();
            fileInput.onchange = function (event) {
                const file = event.target.files[0]
                // if (!formatArr.includes(file.type)) {
                //     reject({ message: false, event })
                // } else 
                {
                    resolve({ message: true, event, file })
                }
            }
        });
    }

    openFile() {
        LocalFileSvc.open();
    }

    newFile() {
        let refNewFileDlg = window.refNewFileDlg.value;
        refNewFileDlg.mainType = 'file';
        refNewFileDlg.titleText = "新建文件";
        refNewFileDlg.visible = true;
    }

    newProject() {
        let refNewFileDlg = window.refNewFileDlg.value;
        refNewFileDlg.mainType = 'project';
        refNewFileDlg.titleText = "新建数据工程";
        refNewFileDlg.visible = true;
    }
}

export default { FileMenu }

